package com.gitee.mrj.aiboot.core.sf.generators.ui.widget;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.widget.SFRateWidget;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFRateWidgetGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFRateWidget widget = AnnotatedElementUtils.findMergedAnnotation(field, SFRateWidget.class);
        generate(node, widget);
    }

    private void generate(UIForm node, SFRateWidget widget) {
        ObjectNode ui = node.getUi();
        ui.put("widget", "rate");
        ui.put("allowClear", widget.allowClear());
        ui.put("autoFocus", widget.autoFocus());
        putString(ui, "text", widget.text());
        if (ArrayUtils.isNotEmpty(widget.tooltips())) {
            ArrayNode array = putArray(ui, "tooltips");
            for (String tooltip : widget.tooltips()) {
                array.add(tooltip);
            }
        }
    }


    @Override
    public Class[] supports() {
        return new Class[]{SFRateWidget.class};
    }
}
